package Homework5;

import java.util.*;

public class IntersectionAndUnion {
    public static ArrayList<Integer> intersection(ArrayList<Integer> list1, ArrayList<Integer>list2){
        ArrayList<Integer> result = new ArrayList<>();

        for (int i = 0; i < list1.size(); i++){
            for (int j = 0; j < list2.size(); j++){
                if (Objects.equals(list1.get(i), list2.get(j))){
                    result.add(list1.get(i));
                    continue;
                }
            }
        }

        Collections.sort(result);

        return result;
    }

    public static ArrayList<Integer> union(ArrayList<Integer> list1, ArrayList<Integer> list2) {
        Set<Integer> set = new HashSet<>();
        set.addAll(list1);
        set.addAll(list2);

        ArrayList<Integer> result = new ArrayList<>(set);
        Collections.sort(result);
        return result;
    }
}

